<?php

class ConfigController extends BackEndController {

    public function accessRules() {
        return array(
            array('allow',
                'roles' => array('super_admin'),
                ),
            array('deny',
                'users' => array('*')
            )
        );
    }
    public function actionIndex() {
        $this->render('index');
    }

    public function actionPrice() {
        if (isset($_POST['Form_price'])) {
            foreach ($_POST['Form_price'] as $key => $val) {
                ConfigData::setConfig($key, (float) $val);
            }
        }
        $prefix = FormData::PRICE_PREFIX . '%';
        $sql = "select * from tbl_config_data cf where cf.path like '{$prefix}'";
        $price = Yii::app()->db->createCommand($sql)
                ->queryAll();
        $this->render('price', array('model' => $price));
    }

    public function actionAuthor() {
        $controllers = Yii::app()->metadata->getActions('ThongbaomoithauController');
        var_dump($controllers);

        $helper = new CFileHelper();
        $path = Yii::getPathOfAlias('webroot') . '/protected/controllers/back';
        foreach ($helper->findFiles($path) as $value) {
            $tmp = explode('/', str_replace('.php', '', $value));
            $arr = explode('\\', $tmp[count($tmp) - 1]);
            $class_name = $arr[count($arr) - 1];
            if ($class_name == get_class($this)) {
                continue;
            }
            $controllers = Yii::app()->metadata->getActions('ThongbaomoithauController');
            var_dump($controllers);
        }
        die;
    }

    public function actionRefreshResource() {
        $helper = new CFileHelper();
        $path = Yii::getPathOfAlias('webroot') . '/protected/controllers/back';
        foreach ($helper->findFiles($path) as $value) {
            $tmp = explode('/', str_replace('.php', '', $value));
            $arr = explode('\\', $tmp[count($tmp) - 1]);
            $class_name = $arr[count($arr) - 1];
            if ($class_name == get_class($this)) {
                continue;
            }
            $actions = Yii::app()->metadata->getActions($class_name);
            foreach ($actions as $action) {
                $controler = trim(strtolower($class_name));
                $action = trim(strtolower($action));
                $resource = Resource::model()->exists('controller = :controller and action = :action',array(':action'=>$action,':controller'=>$controler));
                if(!$resource){
                    $resource = new Resource();
                    $resource->action = $action;
                    $resource->controller = $controler;
                    $resource->save();
                }
            }
        }
    }
    public function actionAccessRule(){
        $roles = RoleResource::getAllRole();
        if(isset($_POST['Resource'])){
            RoleResource::model()->deleteAll();
            foreach ($_POST['Resource'] as $controller => $value) {
                foreach ($value as $action => $arr) {
                    RoleResource::assignResource($controller, $action, $arr);
                }
            }
        }
        $access_rule = Resource::getAccessRule();
        $this->render('accessrule', array('rules' => $access_rule,'roles'=>$roles));
    }

}
